define(function(require, exports, module) {
  var $ = require("$");
  var bootbox = require("bootbox");
  var dtUtil = require('datatableutil');
  require("bootstrap_commonrequire");
  require("moment");
  require('daterangepicker.css');
  require('bootstrap-daterangepicker');
  require('select2.css');
  require('select2');
  var loader = require("./loader/loader");

  exports.init = function(param) {

    $(document).ready(function() {
      // 蒙版
      loader.loading("数据加载中，请稍后……");
      // 加载自定义模板-条件区域
      var template = require("./numberpoolupgrade.tpl");
      var Widget = require("widget");
      var Component = Widget.extend({
        template : template
      });
      new Component({
        renderTo : "queryAreaId"
      });
      
      // 下拉菜单设置
      // 号码状态
      var myDataStatus = [ {
        "id" : "open",
        "text" : "开启"
      }, {
        "id" : "unopen",
        "text" : "停用"
      }, {
        "id" : "reserve",
        "text" : "预留"
      }, {
        "id" : "pricefixed",
        "text" : "已定价"
      } ];
      $('#dataStatustpl').select2({
        placeholder : "请选择号码状态",
        allowClear : true,
        data : myDataStatus
      });
      
      // 选择通话类型
      var myNumberSource = [ {
        "id" : "external",
        "text" : "通道提供"
      }, {
        "id" : "custom",
        "text" : "用户提供"
      }, {
        "id" : "inside",
        "text" : "内部号码"
      }, {
        id : 'general',
        text : "公共号码池"
      } ];
      $('#numberSource').select2({
        placeholder : "请选择号码来源",
        allowClear : true,
        data : myNumberSource
      });
      
      // 初始化归属地，绑定所有省份的名称下拉列表
      $.ajax({
        type : "POST",
        dataType : "json",
        url : "/poolnumber/queryAllAreas.do",
        async : false,
        success : function(data) {
          $("#numberVest").select2({
            placeholder : "请选择省份名称",
            allowClear : true,
            data : data
          });
        },
        error : function(data) {
          bootbox.alertTimeout('初始化省份名称数据失败！');
        }
      });
      
     
      function getCondition() {
        //获取查询条件
        var numbers = $('#numbers').val();// 手机号码
        var dataStatus = $('#dataStatustpl').val();// 号码状态
        var numberSource = $('#numberSource').val();// 号码来源
        var numberVest = $('#numberVest').val();// 号码归属地
        var queryStr = "";

        if (numbers != '' && numbers != null) {
          queryStr += ("tpn.phone_number like '%" + numbers + "%';");
        }
        if (dataStatus != '' && dataStatus != null) {
          queryStr += ("tpn.number_state='" + dataStatus + "';");
        }
        if (numberSource != '' && numberSource != null) {
          queryStr += ("tpn.number_source='" + numberSource + "';");
        }
        if (numberVest != '' && numberVest != null) {
          queryStr += ("tpn.number_vest_name='" + numberVest + "';");
        }
        
        return queryStr;
      }
      
      //加载表单模板和数据
      var Table = require("table");
      // 查询内容显示
      new Table({
        attrs : {
          id : "data_table",
          isHovered : false,
          isStriped : true,
          isBordered : false
        },
        renderTo : param.divWrapperId

      });
      
      // NumberPool信息显示
      var accountinfo_data_table = $('#data_table').dataTable({
        "sDom" : "<'hidden'<'span6'l><'hidden'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
        "bJQueryUI" : false,
        "oLanguage" : {
          "sUrl" : "/business/js/common/table/src/lang/dataTable.cn.txt"
        },
        "sPaginationType" : "bootstrap", // 分页自带两种样式，full_numbers和two_button(默认)
        'bPaginate' : true, // 是否分页
        "bProcessing" : true, // 当datatable获取数据时候是否显示正在处理提示信息。
        "bServerSide" : true, // 指定服务器端分页
        "bDeferRender" : true,
        "bFilter" : true,
        "bSort" : true,
        "bInfo" : true,
        "sAjaxSource" : param.ajaxUrl,
        "iDisplayLength" : DATATABLES_CONFIG.iDisplayLength.DEFAULT, // 默认分页的每页记录数
        "fnServerData" : function(sSource, aoData, fnCallback, oSettings) {
          var queryparam = dtUtil.convert_aoData(aoData);
          
          //拼接where查询条件
          var cond = getCondition();
          if (cond != '' && cond != null) {
            queryparam.q += cond;
          }

          // 添加自己的额外参数
          oSettings.jqXHR = $.ajax({
            "dataType" : 'json',
            "contentType" : "application/json;charset=UTF-8",
            "type" : "GET",
            "url" : sSource,
            "data" : queryparam, // 将请求数据转换为后台要求的JSON格式
            "success" : function(json) {
              if (json.flag == '000000') {
                // 隐藏蒙版
                loader.noloading();
                dtUtil.successCallback(json, aoData, fnCallback);
              } else {
                oauth2Relogin();
              }
            }
          });

        },
        "aoColumns" : [ {
          "sTitle" : "id",
          "bSortable" : false,
          "mData" : "id",
          "bVisible" : false
        }, {
          "sTitle" : "手机号码",
          "bSortable" : false,
          "mData" : "phone_number",
          "bVisible" : true
        }, {
          "sTitle" : "归属地",
          "bSortable" : false,
          "mData" : "number_vest_name",
          "bVisible" : true
        }, {
          "sTitle" : "号码状态",
          "bSortable" : false,
          "mData" : "number_state",
          "mRender" : function(data, type, full) {
              if (data == "open")
                return "开启";
              else if (data == "unopen")
                return "停用";
              else if (data == "reserve")
                return "预留";
              else if (data == "pricefixed")
                return "已定价";
              else
                return "未知";
           }
        }/*, {
          "sTitle" : "能力号码关系",
          "bSortable" : false,
          "mData" : "app_name",
          "sWidth" : "16%",
          "mRender" : function(data, type, full) {
            var ret = '<div>';
            ret += '<a href="javascript:void(0);" class="btn-link" title="查看详情">'+data.substr(0,10)+'...</a>';
            return ret += '</div>';
          }
        }*/, {
          "sTitle" : "备注",
          "bSortable" : false,
          "mData" : "number_comment",
          "bVisible" : true
        }, {
          "sTitle" : "靓号类型",
          "bSortable" : false,
          "mData" : "beautiful_type",
          "bVisible" : false,
          "mRender" : function(data, type, full) {
            if (data == "NOTHING")
              return "无";
            else
              return data;
         }
        }, {
          "sTitle" : "号码来源",
          "bSortable" : false,
          "mData" : "number_source",
          "bVisible" : true,
          "mRender" : function(data, type, full) {
            if (data == "external")
              return "通道提供";
            else if (data == "custom")
              return "用户提供";
            else if (data == "inside")
              return "内部号码";
            else if (data == "general")
              return "公共号码池";
            else
              return "未知";
         }
        }, {
          "sTitle" : "多个账号可重用",
          "bSortable" : false,
          "mData" : "is_resue",
          "bVisible" : true,
          "mRender" : function(data, type, full) {
            if (data == "yes")
              return "可重用";
            else if (data == "no")
              return "不可重用";
            else
              return "未知";
         }
        }, {
          "sTitle" : "操作",
          "bSortable" : false,
          "mData" : "number_comment",
          "mRender" : function(data, status, full) {
            var ret = '<div class="btn-group" style="margin-top: 8px;">';
            ret += '<a href="javascript:void(0);" class="btn icon-reply contrast" title="修改"></a>';
            ret += '<a href="javascript:void(0);" class="btn icon-eraser contrast" title="能力配置"></a>';
            return ret += '</div>';
          }
        }],
        "fnDrawCallback" : function(oSettings) {
          // 表格绘制完成后，去除width属性，使表格可缩放
          $('#' + oSettings.sTableId).css("width", "");
        }
      });

      
      // 查看详情
     /* $("#data_table tbody").on("click", "tr>td>div>a.btn-link", function(e) {
        var rowNode = this.parentNode.parentNode.parentNode;
        var rowData = accountinfo_data_table.fnGetData(rowNode);
        numPoolDetail(rowData.app_name);
      });
      // 查看详情
      var numPoolDetail = function(context) {
        var numPool_detail = require("./numberpooldetails");
        if ($("#numPool_detail_id").length <= 0)
          $("body").append("<div id='numPool_detail_id'></div>");
        numPool_detail.init(
            {
              "title" : "能力号码关系",
              "renderTo" : "numPool_detail_id",
              "details": context
            });

      }*/
      
      //新建号码信息按钮
      $("#creatbtn").on("click", function() {
        $('#accountinfo_data_table').hide();
        $("#accountForm").hide();
        $("#queryAreaId").hide();
        var accountForm = require("./numberpoolupgradeadd");
        accountForm.init("accountinfo_table_edit", null);
        $('#accountinfo_table_edit').show();
      });
      
      // 修改按钮
      $("#data_table tbody").on("click", "tr>td>div>a.icon-reply", function(e) {
        $('#accountinfo_data_table').hide();
        $("#accountForm").hide();
        $("#queryAreaId").hide();
        var rowNode = this.parentNode.parentNode.parentNode;
        var rowData = accountinfo_data_table.fnGetData(rowNode);
        var numberpoolinforeturnform = require("./numberpoolupgradeupdate");
        numberpoolinforeturnform.init("accountinfo_table_edit", rowData);
        $('#accountinfo_table_edit').show();
      });
      
      // 能力配置按钮
      $("#data_table tbody").on("click", "tr>td>div>a.icon-eraser", function(e) {
        $('#accountinfo_data_table').hide();
        $("#accountForm").hide();
        $("#queryAreaId").hide();
        var rowNode = this.parentNode.parentNode.parentNode;
        var rowData = accountinfo_data_table.fnGetData(rowNode);
        var numberpoolinforeturnform = require("./numberpoolupgradeconf");
        numberpoolinforeturnform.init("abilityConfId", rowData);
        $('#abilityConfId').show();
      });
      
      
      //查询按钮
      $('#querybtn').click(function() {
        // 蒙版
        loader.loading("数据加载中，请稍后……");
        accountinfo_data_table.fnFilter("");
        return false;
      });

      //重置按钮
      $('#restbtn').click(function() {
        // 蒙版
        loader.loading("数据加载中，请稍后……");
        $('#numbers').val("");
        $('#dataStatustpl').select2("val", "");
        $('#numberSource').select2("val", "");
        $('#numberVest').select2("val", "");
        accountinfo_data_table.fnFilter("");
        return false;
      });

    });

  };
});
